# 实现自动取款机的存取款模拟效果。要求有登陆和退出、查询余额、取钱、存钱等操作
import os
#建立初始库
user_list= [
    {'username':'Zekai','password':'123456','money':5000000},
    {'username':'Dad','password':'1234567','money':3000000},
    {'username':'Ming','password':'12345678','money':3000000},
]
def check_user(user_name):
    for user in user_list:
        if user_name == user['username']:
            print (user_list.index(user))
            return user_list.index(user)+1

def check_login(user_name, pass_word):
    for user in user_list:
        if user_name == user['username'] and pass_word==user['password']:
            return True

def draw_money(uid):
    money = input('How much do you wanna get:')
    while not money.isdigit():
        money=input('Error, please re-enter the number')
    if int(money)<=user_list[uid]['money']:
        user_list[uid]['money']-=int(money)
        print('-'*30)
        print('Success! You withdraw {:.2f} Yuan, your account still leave {:.2f} Yuan'.format(int(money),user_list[uid]['money']))
        print('-'*30)
    else:
        print('-'*30)
        print('You do not have enough money')
        print('-'*30)

def save_money(uid):
    money = input('please enter the amount you wanna deposit:')
    while not money.isdigit():
        money = input('Error, please re-enter the number')
    user_list[1]['money']+=int(money)
    print('-'*30)
    print('Congrat! You deposit:{:.2f} yuan, your account is {:.2f} yuan now'.format(int(money),user_list[uid]['money'])
def main():
    while True:
        print('='*20,'welcome to the ATM','='*20)
        username = input('Please enter your ID:')
        uid = check_user(username)
        print(uid)
        if uid:
            password = input('Please enter your password:')
            if check_login(username,password):
                print('-'*20)
                print('Successfully log in ')
                print('-'*20)
                while True:
                    print('''1.Check Balance
                             2.Withdrawal
                             3.Deposit
                             4.Quit
                    ''')
                    choice = input('Please enter your instruction:')
                    if choice == '1':
                        print('-'*20)
                        print('Your account has: {:.2f} Yuan'.format(user_list[uid-1]['money']))
                        print('-' * 20)
                    elif choice == '2':
                        draw_money(1)
                    elif choice == '3':
                        save_money(uid-1)
                    elif choice == '4':
                        print('-' * 20)
                        print('Successfully Log out')
                        print('-' * 20)
                        break
                    else:
                        Error
            else:
                print('-' * 20)
                print('Wrong password')
                print('-' * 20)
        else:
            print('-' * 20)
            print('Wrong account')
            print('-' * 20)
main()
